<template>
    <mi-form-page @submit="submit" v-loading="edit.loading">
        <el-form :model="edit.info"
                 :rules="rules"
                 ref="formRef">
            <el-form-item prop="title" label="标题">
                <mi-input v-model="edit.info.title"/>
            </el-form-item>

            <el-form-item prop="content" label="内容">
                <mi-wang-editor v-model="edit.info.content" />
            </el-form-item>

            <el-form-item prop="status" label="状态">
                <mi-switch v-model="edit.info.status"  />
            </el-form-item>
        </el-form>
    </mi-form-page>

</template>

<script setup lang="ts">
import {reactive, watch, ref, onBeforeMount, inject} from "vue";
import {simpleGet} from "@/utils/request";
import {makeModelAuth} from "@/utils/auth";
import {FormInstance} from "element-plus";
import List from "@/model/list";
import Edit from "@/model/edit";
import {useAdminStore} from "@/store/admin";

const auth = makeModelAuth("single")
const rules = {
    title:[{required:true, message:'必填'}],
    content:[{required:true, message:'必填'}],
    status:[{required:true, message:'必填'}],
}
const adminStore = useAdminStore()
const formRef = ref<FormInstance>()
const list = inject<List>("list")
const edit = inject<Edit>("edit", new Edit({}))
const row:any = list?.currentRow
const id = row ? row.id: ''

const submit = () => {
    let url = '/' + (id ? auth.edit : auth.add)
    edit.submit({
        formRef: formRef.value,
        url:url,
        list: list
    })
}


onBeforeMount(() => {
    edit.init(async () => {
        if (id){
            // 请求表单数据
            let res = await simpleGet('/' + auth.edit, {id:id})
            edit.info = res.info
        }else{
            edit.info = {
                status:1,
            }
        }
    })
})

</script>

<style scoped>

</style>
